<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--通用查询-->
      <queryHeader :dict="dict" :permission="permission" />
      <!--通用编辑-->
      <crudOperation :permission="permission" />
    </div>

    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <!--多选框-->
      <el-table-column type="selection" width="55" />
      <!--展示字段-->
      <el-table-column v-if="columns.visible('orderSerial')" prop="orderSerial" label="订单编号" :width="160" />
      <el-table-column v-if="columns.visible('deviceSerial')" prop="deviceSerial" label="设备编号" :width="100" />
      <el-table-column v-if="columns.visible('rentUserName')" prop="rentUserName" label="租户名" :width="160" />
      <el-table-column v-if="columns.visible('userPhone')" prop="userPhone" label="用户手机号" :width="100" />
      <el-table-column v-if="columns.visible('hospitalName')" prop="hospitalName" label="医院名称" :width="180" />
      <el-table-column v-if="columns.visible('buildingName')" prop="buildingName" label="楼栋名称" :width="80" />
      <el-table-column v-if="columns.visible('floorName')" prop="floorName" label="楼层名称" :width="80" />
      <el-table-column v-if="columns.visible('departName')" prop="departName" label="科室名称" :width="100" />
      <el-table-column v-if="columns.visible('orderTime')" prop="orderTime" label="开单时间" :width="140" />
      <el-table-column v-if="columns.visible('orderEndTime')" prop="orderEndTime" label="结单时间" :width="140" />
      <el-table-column v-if="columns.visible('useLength')" prop="useLength" label="使用时长" :formatter="formatUseLength" :width="120" />
      <el-table-column v-if="columns.visible('priceDesc')" prop="priceDesc" label="价格描述" :width="230" />
      <el-table-column v-if="columns.visible('orderRemark')" prop="orderRemark" label="订单备注" :width="160" />
      <el-table-column v-if="columns.visible('orderAmount')" prop="orderAmount" label="订单金额" :width="70" fixed="right" />
      <el-table-column v-if="columns.visible('orderAmountPay')" prop="orderAmountPay" label="支付金额" :width="70" fixed="right" />
      <el-table-column v-if="columns.visible('orderStatus')" prop="orderStatus" label="订单状态" :show-overflow-tooltip="true" fixed="right">
        <template slot-scope="scope">
          {{ dict.label.status_order[scope.row.orderStatus] }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('payStatus')" prop="payStatus" label="支付状态" :width="70" fixed="right">
        <template slot-scope="scope">
          {{ dict.label.status_order_pay[scope.row.payStatus] }}
        </template>
      </el-table-column>
      <!--操作-->
      <el-table-column label="操作" width="130px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button v-if="scope.row.orderStatus === 1" size="mini" type="text" @click="finish(scope.row)">
            结束订单
          </el-button>
          <el-button v-if="scope.row.orderStatus === 2 && scope.row.payStatus === 1" size="mini" type="text" @click="free(scope.row)">
            免单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <pagination />

    <!--表单渲染-->
    <editForm :dict="dict" />
  </div>
</template>

<script>
import crudOrder, { finishOrder, freeOrder } from '@/api/business/bed/order'
import queryHeader from './module/queryHeader'
import editForm from './module/editForm'
import CRUD, { presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'

// crud交由presenter持有
const crud = CRUD({ title: 't_order', url: 'order/list/page/token', crudMethod: { ...crudOrder }, pkIdName: 'orderId', queryOnPresenterCreated: false })

export default {
  name: 'Order',
  components: { queryHeader, editForm, crudOperation, pagination },
  dicts: ['status_order', 'status_order_pay'],
  mixins: [presenter(crud)],
  data() {
    return {
      // 对应几个按钮操作
      permission: {
        add: [],
        edit: [],
        del: []
      }
    }
  },
  created() {
    this.crud.optShow = { add: false, edit: false, del: false, download: true }

    this.crud.params.rentUserId = this.$route.query.rentUserId

    this.crud.toQuery()
  },
  methods: {
    formatUseLength(row) {
      return this.SecondToDate(row.useLength)
    },
    finish(row) {
      finishOrder(row.orderSerial).then(() => {
        this.$notify({ title: '操作成功', type: 'success', duration: 2500 })
        this.crud.toQuery()
      })
    },
    free(row) {
      freeOrder(row.orderSerial).then(() => {
        this.$notify({ title: '操作成功', type: 'success', duration: 2500 })
        this.crud.toQuery()
      })
    },
    [CRUD.HOOK.beforeRefresh]() {
      if (crud.query.dateScope) {
        crud.query.startTime = crud.query.dateScope[0]
        crud.query.endTime = crud.query.dateScope[1]
      }
    },
    SecondToDate: function(msd) {
      var time = msd

      if (time != null && time !== '') {
        if (time > 60 && time < 60 * 60) {
          time = parseInt(time / 60.0) + '分钟' + parseInt((parseFloat(time / 60.0) -

            parseInt(time / 60.0)) * 60) + '秒'
        } else if (time >= 60 * 60 && time < 60 * 60 * 24) {
          time = parseInt(time / 3600.0) + '小时' + parseInt((parseFloat(time / 3600.0) -

            parseInt(time / 3600.0)) * 60) + '分钟' +

            parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -

              parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + '秒'
        } else if (time >= 60 * 60 * 24) {
          time = parseInt(time / 3600.0 / 24) + '天' + parseInt((parseFloat(time / 3600.0 / 24) -

            parseInt(time / 3600.0 / 24)) * 24) + '小时' + parseInt((parseFloat(time / 3600.0) -

            parseInt(time / 3600.0)) * 60) + '分钟' +

            parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -

              parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + '秒'
        } else {
          time = parseInt(time) + '秒'
        }
      }

      return time
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
